-- global_pure_damage
-- Created by chengb Nov/13/2015
-- 技能: 全体伤害，对全体怪物施加纯伤害（不考虑加成等因素）
-- 目前支持如下配置：
-- gobal_pure_damage(800, 200)：对全体怪物造成800的魔法伤害，技能每提升1级，效果增加200。（最终结果还要除以1000）

return {
    apply = function(source, target, skillId, round, para, extra)
        local base   = para[1];
        local growth = para[2];
        local level  = extra["level"] or 0; 
       
        -- 攻击方的魔力
        local magic = source:getMagic();
        
        -- 最终伤害
        local damage = math.modf(magic * (base + level * growth) / 1000);
        
        -- 找出所有存活的怪
        local ret = {};
        for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
            local grid = DungeonM.getGridByPos(p);

            -- 是未死亡的怪物
            if grid:isOpened() and
               grid:isMonster() and
               not grid.monster:isDead() then
                table.insert(ret, grid.monster);
            end
        end
        
        local totalDamage = 0;
        for _, monster in pairs(ret) do
            -- 增加命中动作
            if SkillM.getSequence(monster) == nil then
                SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
            end

            SkillM.getSequence(monster):hit(source, monster, skillId);

            -- 造成伤害
            CombatM.receiveDamage(source, monster, damage, skillId, extra);
           
            totalDamage = totalDamage + damage;
        end

        -- 如果是玩家，抛出攻击结果事件
        if source.type == OBJECT_TYPE_USER then
            EventMgr.fire(event.HERO_ATTACK_RESULT, { ["damage"] = totalDamage, ["extra"] = extra});
        end
        
        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        
        return { tonumber(arr[1]), tonumber(arr[2])};
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local base   = para[1];
        local growth = para[2];
        local level  = SkillM.getSkillLevel(skillId);
        
        local magic = source:getMagic();
        
        -- 最终伤害
        local damage = math.modf(magic * (base + level * growth) / 1000);

        desc = string.gsub(desc, "{damage}", damage);

        return desc;
    end,
};
